﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FinalOnlineLibrary.Models;
using OnlineLibrary.ToLinq;
using MoonLight.Common;

namespace FinalOnlineLibrary.DAL
{
    public static class DALBooks
    {
        /// <summary>
        /// Select top 5 new books in tblResource
        /// </summary>
        /// <returns></returns>
        public static IQueryable<tblResource> SelectTopBooks()
        {
            IQueryable<tblResource> showBooks = null;
            try
            {
                showBooks = (from book in DataProvider.Db.tblResources
                                 orderby book.ResourceId descending
                                 select book).Take(5);
            }catch(Exception ex)
            {
                AppLogger.Error("Error : Can not connect database(DataContext)", ex, "FinalOnlineLibrary.DAL.DALBooks", "SelectTopBooks");
                
            }
            return showBooks;
        }
        /// <summary>
        /// Select top 5 new books in tblResource with constraint is ResType
        /// </summary>
        /// <param name="resType"></param>
        /// <returns></returns>
        public static IQueryable<tblResource> SelectTopBooks(int resType) 
        {
            IQueryable<tblResource> showBooks = null;
            try
            {
                showBooks = (from book in DataProvider.Db.tblResources
                                 where book.ResType == resType
                                 orderby book.ResourceId descending
                                 select book).Take(5);
            }
            catch (Exception ex)
            {
                AppLogger.Error("Error : Can not connect database(DataContext)",
                    ex, "FinalOnlineLibrary.DAL.DALBooks", "SelectTopBooks(int resType)");
            }
            return showBooks;
        }

        /// <summary>
        /// Get ResTypeName in tblResType
        /// </summary>
        /// <param name="resTypeId"></param>
        /// <returns></returns>
        public static string SelectResTypeName(int resTypeId) 
        {
            string resTypeName = string.Empty;
            try
            {
                resTypeName = (from book in DataProvider.Db.tblResTypes
                                      where book.ResTypeId == resTypeId
                                      select book.ResTypeName).Single();
            }
            catch (Exception ex) 
            {
                AppLogger.Error("Error : Can not connect database(DataContext)",
                    ex, "FinalOnlineLibrary.DAL.DALBooks", "SelectResTypeName(int resTypeId)");
            }
            return (resTypeName);
        }
        /// <summary>
        /// Search book from tblResource
        /// </summary>
        /// <param name="keySearch"></param>
        /// <returns></returns>
        public static IQueryable<tblResource> SearchBooks(string keySearch, int resType, int catType) 
        {
            IQueryable<tblResource> searchResult = null;
            try
            {
                searchResult = from book in DataProvider.Db.tblResources
                               where book.ResourceName.Contains(keySearch) &&
                                        book.ResType == resType &&
                                        book.ResourceCat == catType
                               orderby book.ResourceId descending
                               select book;
            }
            catch (Exception ex) 
            {
                AppLogger.Error("Error : Can not connect database(DataContext)",
                    ex, "FinalOnlineLibrary.DAL.DALBooks", "SearchBooks(string keySearch, int resType, int catType)");
            }
            return searchResult;
        }
        /// <summary>
        /// Get all Resource Type in tbResType
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<tblResType> GetAllResType() 
        {
            IEnumerable<tblResType> getResult = null;
            try
            {
                getResult = from resType in DataProvider.Db.tblResTypes
                            select resType;
            }
            catch (Exception ex)
            {
                AppLogger.Error("Error : Can not connect database(DataContext)",
                    ex, "FinalOnlineLibrary.DAL.DALBooks", "GetAllResType()");    
            }
            return getResult;
        }

        /// <summary>
        /// Get all Resource Category in tblResCategory
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<tblResCategory> GetAllCategory() 
        {
            IEnumerable<tblResCategory> getResult = null;
            try
            {
                getResult = from catType in DataProvider.Db.tblResCategories
                            select catType;
            }
            catch (Exception ex) 
            {
                AppLogger.Error("Error : Can not connect database(DataContext)",
                    ex, "FinalOnlineLibrary.DAL.DALBooks", "GetAllCategory()");     
            }
            return getResult;
        }

        public static tblUser GetUser(string username,string password)
        {           
            var check_login = (from p in DataProvider.Db.tblUsers
                              where
                                  p.UserName == username &&
                                  p.PassWord == password
                              select p).SingleOrDefault();
            return check_login;
        }
        public static IEnumerable<tblRole> GetAllRoles()
        {
            var getResult = from role in DataProvider.Db.tblRoles
                            select role;

            return getResult;
        }
    }
}